const router = require("koa-router")();
router.prefix("/mail");
const { query, insert } = require("../utils/db");
const {
  MAIL_INSERT_REASON0_WITH_VISITTIME_SQL,
  MAIL_INSERT_REASON0_WITH_STAGENAME_SQL,
  MAIL_INSERT_REASON1_WITH_VISITTIME_SQL,
  MAIL_INSERT_REASON1_WITH_STAGENAME_SQL,
  MAIL_QUERY_SQL,
  PRICE_QUERY_SQL,
} = require("./../utils/sql");
const { formatDate } = require("../utils/formatDate");
router.post("/insertMailReason0WithVisitTime", async (ctx, next) => {
  const {
    reason,
    userName,
    mailUserName,
    mailUserPhone,
    mailAddress,
    pickUpUserName,
    pickUpUserPhone,
    pickUpAddress,
    mode,
    visitTime,
    goodsInformation,
  } = ctx.request.body;
  if (
    userName &&
    mailUserName &&
    pickUpUserName &&
    visitTime &&
    goodsInformation
  ) {
    const placeOrderTime = formatDate(new Date(), "yyyy年MM月dd日 hh点mm分");
    const queryParams = [
      reason,
      userName,
      placeOrderTime,
      mailUserName,
      mailUserPhone,
      mailAddress,
      pickUpUserName,
      pickUpUserPhone,
      pickUpAddress,
      mode,
      visitTime,
      goodsInformation,
    ];
    const data = await insert(
      MAIL_INSERT_REASON0_WITH_VISITTIME_SQL,
      queryParams
    );
    if (data) {
      ctx.body = {
        status: 200,
        msg: "更新mail表成功",
        data,
      };
    } else {
      ctx.body = {
        status: 500,
        msg: "更新mail表失败",
      };
    }
  }
});
router.post("/insertMailReason0WithStageName", async (ctx, next) => {
  const {
    reason,
    userName,
    mailUserName,
    mailUserPhone,
    mailAddress,
    pickUpUserName,
    pickUpUserPhone,
    pickUpAddress,
    mode,
    stageName,
    goodsInformation,
  } = ctx.request.body;
  if (
    userName &&
    mailUserName &&
    pickUpUserName &&
    stageName &&
    goodsInformation
  ) {
    const placeOrderTime = formatDate(new Date(), "yyyy年MM月dd日 hh点mm分");
    const queryParams = [
      reason,
      userName,
      placeOrderTime,
      mailUserName,
      mailUserPhone,
      mailAddress,
      pickUpUserName,
      pickUpUserPhone,
      pickUpAddress,
      mode,
      stageName,
      goodsInformation,
    ];
    const data = await insert(
      MAIL_INSERT_REASON0_WITH_STAGENAME_SQL,
      queryParams
    );
    if (data) {
      ctx.body = {
        status: 200,
        msg: "更新mail表成功",
        data,
      };
    } else {
      ctx.body = {
        status: 500,
        msg: "更新mail表失败",
      };
    }
  }
});
router.post("/insertMailReason1WithVisitTime", async (ctx, next) => {
  const {
    reason,
    userName,
    mailUserName,
    mailUserPhone,
    mailAddress,
    enterpriseName,
    enterprisePhone,
    enterpriseAddress,
    mode,
    visitTime,
    goodsInformation,
  } = ctx.request.body;
  if (
    userName &&
    mailUserName &&
    enterpriseName &&
    visitTime &&
    goodsInformation
  ) {
    const placeOrderTime = formatDate(new Date(), "yyyy年MM月dd日 hh点mm分");
    const queryParams = [
      reason,
      userName,
      placeOrderTime,
      mailUserName,
      mailUserPhone,
      mailAddress,
      enterpriseName,
      enterprisePhone,
      enterpriseAddress,
      mode,
      visitTime,
      goodsInformation,
    ];
    const data = await insert(
      MAIL_INSERT_REASON1_WITH_VISITTIME_SQL,
      queryParams
    );
    if (data) {
      ctx.body = {
        status: 200,
        msg: "更新mail表成功",
        data,
      };
    } else {
      ctx.body = {
        status: 500,
        msg: "更新mail表失败",
      };
    }
  }
});
router.post("/insertMailReason1WithStageName", async (ctx, next) => {
  const {
    reason,
    userName,
    mailUserName,
    mailUserPhone,
    mailAddress,
    enterpriseName,
    enterprisePhone,
    enterpriseAddress,
    mode,
    stageName,
    goodsInformation,
  } = ctx.request.body;
  if (
    userName &&
    mailUserName &&
    enterpriseName &&
    stageName &&
    goodsInformation
  ) {
    const placeOrderTime = formatDate(new Date(), "yyyy年MM月dd日 hh点mm分");
    const queryParams = [
      reason,
      userName,
      placeOrderTime,
      mailUserName,
      mailUserPhone,
      mailAddress,
      enterpriseName,
      enterprisePhone,
      enterpriseAddress,
      mode,
      stageName,
      goodsInformation,
    ];
    const data = await insert(
      MAIL_INSERT_REASON1_WITH_STAGENAME_SQL,
      queryParams
    );
    if (data) {
      ctx.body = {
        status: 200,
        msg: "更新mail表成功",
        data,
      };
    } else {
      ctx.body = {
        status: 500,
        msg: "更新mail表失败",
      };
    }
  }
});
router.post("/queryMail", async (ctx, next) => {
  const { userName } = ctx.request.body;
  if (userName) {
    const queryParams = [userName];
    const data = await query(MAIL_QUERY_SQL, queryParams);
    if (data) {
      ctx.body = {
        status: 200,
        msg: "查询mail表成功",
        data,
      };
    } else {
      ctx.body = {
        status: 500,
        msg: "查询mail表失败",
      };
    }
  }
});
router.get("/getPrice", async (ctx, next) => {
  const data = await query(PRICE_QUERY_SQL);
  if (data) {
    ctx.body = {
      status: 200,
      msg: "查询物流价格成功",
      data,
    };
  } else {
    ctx.body = {
      status: 500,
      msg: "查询物流价格失败",
    };
  }
});
module.exports = router;
